/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aptech.session.beans;

import entities.Event;
import entities.Prize;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author Chien
 */
@Stateless
public class PrizeFacade extends AbstractFacade<Prize> {

    @PersistenceContext(unitName = "BluePumpkin-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PrizeFacade() {
        super(Prize.class);
    }

    public List<Prize> getPrizeListByEvent(String eventid) {
        String hql = "SELECT p FROM Prize p WHERE p.eventId = :eventId";
        Query query = em.createQuery(hql);
        query.setParameter("eventId", eventid);
        return (List<Prize>) query.getResultList();
    }

    public List<Prize> findByEvent(String eventid) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Get prize list in 30 days ago.
     *
     * @return List<Prize>
     */
    public List<Prize> getPrizeListWinthinAmonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 1);
        Date toDate = cal.getTime();
        cal.add(Calendar.DATE, -31);
        Date fromDate = cal.getTime();
        String sql = "SELECT p FROM Prize p WHERE p.prizeDate >= :fromDate AND p.prizeDate <= :toDate AND p.isDeleted = :isDeleted";
        TypedQuery<Prize> createQuery = em.createQuery(sql, Prize.class);
        createQuery.setParameter("fromDate", fromDate);
        createQuery.setParameter("toDate", toDate);
        createQuery.setParameter("isDeleted", false);
        return createQuery.getResultList();
    }

    public List<Prize> getPrizeListByEvent(Event event) {
        String sql = "SELECT p FROM Prize p WHERE  p.isDeleted = :isDeleted AND p.eventId= :event";
        TypedQuery<Prize> createQuery = em.createQuery(sql, Prize.class);

        createQuery.setParameter("event", event);
        createQuery.setParameter("isDeleted", false);
        return createQuery.getResultList();
    }
}
